$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie('mid');
    var token = $.cookie('token');
    localStorage.removeItem("work_time");//localStorage清除预约时间
    localStorage.removeItem("doc_id");//localStorage清除预约医生id
    var databack = false;
    //tab日期
    // var mydate = new Date();
    // var year = mydate.getFullYear();
    // var month = mydate.getMonth() + 1;
    // var date = mydate.getDate();
    // var week = mydate.getDay();
    // var arr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六', '周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    // for (var i = 0; i < 7; i++) {
    //     $('.date_list li').eq(i).find('.week').html(arr[week + i + 1]);
    //     $('.date_list li').eq(i).find('.month_date').html(month + '-' + (date + i + 1));
    //     $('.date1 li').eq(i).find('.week').html(arr[week + i + 1]);
    //     $('.date1 li').eq(i).find('.month_date').html(month + '-' + (date + i + 1))
    // }
    // $('.date_active').on('click', function () {
    //     if (databack) {
    //         $(this).siblings().find('div').removeClass('tag');
    //         $(this).find('div').addClass('tag')
    //     }
    // });
    //指定医师的信息
    $(".load").css("marginTop", "-84px");
    $.get(changeOrderURL + location.search + "&mid=" + mid + "&token=" + token, function (data) {
        var aid = data.aid;
        $(".load").css("display", "none");
        $(".list").html(template("doctor", data));
        //点击首页预约按钮
        //点击弹窗预约
        $('.yuyue').on('click', function () {
            $('.date1_list').html('');
            event.preventDefault();
            var self = $(this);
            var id = self.attr('doctor_id');//医生id
            localStorage.doc_id=id;//localStorage医生ID
            $('#all').show();
            $('body').css('position', 'fixed');
            var myScroll = new iScroll('wrapper', {
                vScroll: false,
                hScroll: true,
                hScrollbar: false
            });
            //显示预约时间段
            $.get(yuyue + "?id=" + id + "&key=1&mid=" + mid + "&token=" + token, function (data) {
                databack = true;
                $(".wait_tip").text("选择时间段");
                $(".choose").html(template("choose_date", data));
                var html=template('yuyue',data);
                $('.box_top').html(html);
                var html=template('date',data);
                $('.date1_list').html(html);
                $('.date1_list li').eq(0).find('div').addClass('tag');
                $('.date_active').on('click', function () {
                    $(this).siblings().find('div').removeClass('tag');
                    $(this).find('div').addClass('tag')

                });
                $(".date_active").on("click", function () {

                    if (databack) {
                        $(".wait_tip").text("请稍等...");
                        $(".choose").html("");
                        databack = false;
                        $.get(yuyue + "?id=" + id + "&key=" + ($(this).index() + 1) + "&mid=" + mid + "&token=" + token, function (data) {
                            databack = true;
                            $(".wait_tip").text("选择时间段");
                            $(".choose").html(template("choose_date", data));
                            var childs = $('.choose').find('li');
                            if (childs.length == 0) {
                                $('.choose').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 60px; color: #666;">暂无可选预约时间</p>')
                            } else {
                                $(".yy").on("click", function () {
                                    if (databack) {
                                        var key = $('#all .tag').parent().index() + 1;
                                        var time = $(this).attr('work_time');
                                        localStorage.setItem("work_time", time);//localStorage预约时间
                                        localStorage.setItem("doc_id", id);//localStorage预约医生id
                                        var mtime = year + '-' + month + '-' + (parseInt(date) + parseInt(key)) + ' ' + time;
                                        databack = false;
                                        $.post(changeOrderURL, {
                                            aid: aid,
                                            mark_time: mtime,
                                            mid: mid,
                                            token: token
                                        }, function (res) {
                                            databack = true;
                                            if (res['state'] == 1) {
                                                $("#all").hide();
                                                creatLayout(res.msg);
                                                setTimeout(function () {
                                                    window.location.href = 'my-appoint.html';
                                                }, 500)
                                            } else {
                                                creatLayout(res.msg);
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
                var childs = $('.choose').find('li');
                if (childs.length == 0) {
                    $('.choose').append('<p style="text-align: center;padding-top: 10px;font-size:15px; line-height: 60px; color: #666;">暂无可选预约时间</p>')
                } else {
                    $(".yy").on("click", function () {
                        if (databack) {
                            var key = $('#all .tag').parent().index() + 1;
                            var time = $(this).attr('work_time');
                            localStorage.setItem("work_time", time);//localStorage预约时间
                            localStorage.setItem("doc_id", id);//localStorage预约医生id
                            var mtime = year + '-' + month + '-' + (parseInt(date) + parseInt(key)) + ' ' + time;
                            databack = false;
                            $.post(changeOrderURL, {
                                aid: aid,
                                mark_time: mtime,
                                mid: mid,
                                d_id:id,
                                token: token
                            }, function (res) {
                                databack = true;
                                if (res['state'] == 1) {
                                    creatLayout(res.msg);
                                    setTimeout(function() {window.location.href = 'my-appoint.html';},2000);
                                } else {
                                    creatLayout(res.msg);
                                }
                            });
                        }
                    });
                }
            });

            //关闭弹框
            $('.close').on('click', function () {
                $('#all').hide();
                $('body').css('position', 'relative');
            });
        });
    });
});

